#!/bin/sh

patch_wrapper()
{
	cd $(dirname "$LINUX_SRC_DIR/$1")
	echo "Patching $LINUX_SRC_DIR/$1 with $ALGA_SRC_DIR/$1.patch"
	#removed for busybox version of patch
	#patch --forward --unified --reject-file=- --input="$ALGA_SRC_DIR/$1.patch"
	patch -N -i "$ALGA_SRC_DIR/$1.patch"
	patch_r=$?
	if test $patch_r -ne 0; then
		echo "Something went wrong when applying $ALGA_SRC_DIR/$1.patch"
		if test $patch_r -eq 1; then
			echo "patch probably already applied, proceeding"
		else
			echo "patch got something really wrong, aborting"
			exit 1
		fi
	fi
}

#=======

if test -z "$1"; then
	echo "Missing the target linux source tree"
	exit 1
fi

LINUX_SRC_DIR=$1
ALGA_SRC_DIR=$PWD

patch_wrapper include/drm/drm_pciids.h

patch_wrapper drivers/video/Kconfig

cp -R "$ALGA_SRC_DIR/drivers/gpu/alga" "$LINUX_SRC_DIR/drivers/gpu/"

patch_wrapper drivers/gpu/Makefile

cp -R "$ALGA_SRC_DIR/include/alga" "$LINUX_SRC_DIR/include/"

#=======
#shared headers with userland

patch_wrapper include/uapi/Kbuild

cp -R "$ALGA_SRC_DIR/include/uapi/alga" "$LINUX_SRC_DIR/include/uapi/"
